package com.zjb.demo.system.service;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.util.DateUtil;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.zjb.demo.common.domain.dto.SysOperationLog;
import com.zjb.demo.system.domain.query.SysOperationLogQuery;
import com.zjb.demo.system.mapper.SysOperationLogMapper;
import org.noear.solon.annotation.Component;

import java.util.List;

import static com.zjb.demo.common.domain.dto.table.SysOperationLogTableDef.SYS_OPERATION_LOG;

/**
 * @author 庄佳彬
 * @since 2023/12/25 22:47
 */
@Component
public class SysOperationLogService extends ServiceImpl<SysOperationLogMapper, SysOperationLog> {
    /**
     * 根据查询条件分页查询操作日志
     *
     * @param query 查询条件
     * @return 分页结果
     */
    public Page<SysOperationLog> pageList(SysOperationLogQuery query) {
        var page = new Page<SysOperationLog>(query.getPageNum(), query.getPageSize());
        var start = DateUtil.parseDate(query.getBeginTime());
        var end = DateUtil.parseDate(query.getEndTime());
        return queryChain()
                .select(SYS_OPERATION_LOG.ALL_COLUMNS)
                .from(SYS_OPERATION_LOG)
                .where(SYS_OPERATION_LOG.TITLE.like(query.getTitle()))
                .and(SYS_OPERATION_LOG.BUSINESS_TYPE.eq(query.getBusinessType()))
                .and(SYS_OPERATION_LOG.BUSINESS_TYPE.in(query.getBusinessTypes()))
                .and(SYS_OPERATION_LOG.STATUS.eq(query.getStatus()))
                .and(SYS_OPERATION_LOG.OPER_NAME.like(query.getOperName()))
                .and(SYS_OPERATION_LOG.OPER_TIME.ge(start))
                .and(SYS_OPERATION_LOG.OPER_TIME.le(end))
                .orderBy(SYS_OPERATION_LOG.OPER_ID.desc())
                .page(page);
    }


    /**
     * 根据给定的查询条件获取操作日志列表
     *
     * @param query 操作日志查询条件
     * @return 操作日志列表
     */
    public List<SysOperationLog> list(SysOperationLogQuery query) {
        var start = DateUtil.parseDate(query.getBeginTime());
        var end = DateUtil.parseDate(query.getEndTime());
        return queryChain()
                .select(SYS_OPERATION_LOG.ALL_COLUMNS)
                .from(SYS_OPERATION_LOG)
                .where(SYS_OPERATION_LOG.TITLE.like(query.getTitle()))
                .and(SYS_OPERATION_LOG.BUSINESS_TYPE.eq(query.getBusinessType()))
                .and(SYS_OPERATION_LOG.BUSINESS_TYPE.in(query.getBusinessTypes()))
                .and(SYS_OPERATION_LOG.STATUS.eq(query.getStatus()))
                .and(SYS_OPERATION_LOG.OPER_NAME.like(query.getOperName()))
                .and(SYS_OPERATION_LOG.OPER_TIME.ge(start))
                .and(SYS_OPERATION_LOG.OPER_TIME.le(end))
                .orderBy(SYS_OPERATION_LOG.OPER_ID.desc())
                .list();
    }

    /**
     * 清理方法
     */
    public void clean() {
        mapper.clean();
    }

}
